<template>
  <div class="container">
    <div class="search-box">
      <el-input v-model="from.value" placeholder="请输入内容" class="input-box"></el-input>
      <el-button @click="clearSearch()">清除</el-button>
      <el-button type="primary" @click="search()">搜索</el-button>
    </div>

    <el-table border style="width: 100%" :data="tasklist">
      <el-table-column type="index" label="任务编号" width="80" :index="indexMethod">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="Edittask(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="deltask(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :current-page="params._page"
        :page-size="params._limit"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="opendialog">+ 新增</el-button>
    </div>

    <el-dialog title="提示" :visible="dialogFormVisible" width="30%" @close="closedialog">
      <el-form :model="fromData" :rules="rules" ref="addFrom">
        <el-form-item label="任务名称" label-width="80px" prop="name">
          <el-input v-model="fromData.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" label-width="80px" prop="des">
          <el-input v-model="fromData.des"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closedialog">取 消</el-button>
        <el-button type="primary" @click="Addtask">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  SearchTaskAPI,
  TaskListAPI,
  addTaskAPI,
  updateTaskAPI,
  delTaskAPI,
  updateAddTaskAPI
} from '@/api/task.js'

import { v4 as uuidv4 } from 'uuid'
export default {
  data() {
    return {
      fromData: {
        name: '',
        des: '',
        id: null
      },
      id: uuidv4(),
      from: {
        value: ''
      },

      params: {
        _page: 1,
        _limit: 5
      },

      total: null,

      tasklist: [],

      dialogFormVisible: false,

      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        des: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {
    this.taskTist()
  },

  methods: {
    // 获取列表
    async taskTist() {
      const res = await TaskListAPI(this.params)
      this.tasklist = res.data
      this.total = Number(res.headers['x-total-count'])
    },

    // 打开弹层
    opendialog() {
      this.dialogFormVisible = true
    },
    closedialog() {
      this.dialogFormVisible = false
      this.$refs.addFrom.resetFields()
    },
    // 分页
    pageChange(page) {
      this.params._page = page
      this.taskTist()
    },
    indexMethod(index) {
      return index + 1 + (this.params._page - 1) * this.params._limit
    },

    // 点击弹层中的确定添加任务
    Addtask() {
      this.$refs.addFrom.validate(async valid => {
        if (valid) {
          if (this.fromData.id) {
            await updateAddTaskAPI(this.fromData)
            this.$message.success('编辑任务成功')
          } else {
            this.fromData.id = this.id
            await addTaskAPI(this.fromData)
            this.$message.success('新增任务成功')
          }
          this.id = uuidv4()
          this.dialogFormVisible = false
          this.taskTist()
          this.$refs.addFrom.resetFields()
          this.fromData = {
            name: '',
            des: '',
            id: ''
          }
        }
      })
    },

    // 点击编辑数据回显
    async Edittask(id) {
      this.dialogFormVisible = true
      const res = await updateTaskAPI(id)
      this.fromData = res.data
    },

    // 点击删除
    async deltask(id) {
      console.log(id)
      await delTaskAPI(id)
      this.$message.success('删除成功')
      this.taskTist()
    },
    // 搜索
    async search() {
      // 判断当前的输入框有没有value
      if (this.from.value) {
        // 调接口
        const res = await SearchTaskAPI(this.from.value)
        console.log(res)
        // 将得到的结果赋值给table列表
        this.tasklist = res.data
        // 获取到的总数赋值给total1
        this.total = res.data.length
      } else {
        //判断
        this.$message.warning('输入的内容不能为空')
      }
    },
    // 清除
    clearSearch() {
      // 将输入框中的值设置为空
      // 重新调用列表
      this.from.value = ''
      this.taskTist()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
